package com.wx.mall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.vo.user.GrowthReqVo;
import com.wx.mall.user.controller.UserController;
import com.wx.mall.user.entity.Growth;
import com.wx.mall.user.entity.User;
import com.wx.mall.user.mapper.GrowthMapper;
import com.wx.mall.user.mapper.UserMapper;
import com.wx.mall.user.service.IGrowthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.user.service.IUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 成长值表 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-03-18
 */
@Service
public class GrowthServiceImpl extends ServiceImpl<GrowthMapper, Growth> implements IGrowthService {

           @Resource
       IUserService service;

    @Override
    public List<Growth> getListByUserId(Integer growthUserId) {

        QueryWrapper<Growth> wrapper=new QueryWrapper<>();
        wrapper.eq("growth_user_id",growthUserId);
        List<Growth> list=this.list(wrapper);
        return list;
    };

    @Override
    public List<Growth> getListByUserIdPage(Integer pageNum, Integer pageSize, Integer growthUserId) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Growth> wrapper=new QueryWrapper<>();
        wrapper.eq("growth_user_id",growthUserId);
        List<Growth> list=this.list(wrapper);
        return list;
    }

    @Override
    public List<Growth> getListByUserIdPageSelect(Integer pageNum, Integer pageSize, Integer growthUserId, String growthSource, Date growthCreateTime) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Growth> wrapper=new QueryWrapper<>();
        wrapper.eq("growth_user_id",growthUserId);
        //        成长值来源
        if(growthSource!=null){
            wrapper.eq("growth_source",growthSource);
        };
//      创建时间
        if(growthCreateTime!=null){
            wrapper.eq("growth_create_time",growthCreateTime);
        };
        List<Growth> list=this.list(wrapper);
        return list;
    }

    @Override
    public List<Growth> getListByOder(Integer pageNum, Integer pageSize,Integer growthUserId, Integer oder) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Growth> wrapper=new QueryWrapper<>();
        if (oder==0){
            wrapper.eq("growth_user_id",growthUserId).orderBy(true,true,"growth_source");

        }else {
            wrapper.eq("growth_user_id",growthUserId).orderBy(true,false,"growth_source");
        }

        List<Growth> list=this.list(wrapper);
        return list;
    };


    @Override
    public List<Growth> getListByTime(Integer pageNum, Integer pageSize,Integer growthUserId, Integer oder) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Growth> wrapper=new QueryWrapper<>();
        if (oder==0){
            wrapper.eq("growth_user_id",growthUserId).orderBy(true,true,"growth_create_time");

        }else {
            wrapper.eq("growth_user_id",growthUserId).orderBy(true,false,"growth_create_time");
        }

        List<Growth> list=this.list(wrapper);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addList(User user, Growth growth) {

        boolean rr= service.updateById(user);
        boolean rs= this.save(growth);
        boolean success=false;
        if(rr==true){
            if (rs==true){
                success=true;
            }
        };

        return success?1:0;
    };

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addManyList(GrowthReqVo vo) {
        List<User> userList=new ArrayList<>();
        for (Long userId:vo.getIds()
             ) {
            User user=new User();
            user.setUserId(userId);
            User list=service.getById(userId);
            Integer muber=list.getUserGrowth();
            //添加
            if(vo.getType()==0){
                 muber=vo.getGrowthChange()+list.getUserGrowth();
            };

//减少
            if(vo.getType()==1){
                muber=list.getUserGrowth()-vo.getGrowthChange();
            };

           user.setUserGrowth(muber);
           userList.add(user);
        };

        boolean rr= service.updateBatchById(userList);
//        处理添加成长值
        List<Growth> growthList=new ArrayList<>();

        for (Long userId:vo.getIds()
        ) {
           Growth growth=new Growth();
           growth.setGrowthUserId(userId);
           Integer value=0;
            //添加
            if(vo.getType()==0){
               value= vo.getGrowthChange();
              growth.setGrowthChange(value);
            };

//减少
            if(vo.getType()==1){
                value= -vo.getGrowthChange();
                growth.setGrowthChange(value);
            };

             growth.setGrowthRemark(vo.getGrowthRemark());
             growth.setGrowthSource("管理员");
            growthList.add(growth);
        }

        boolean rs= this.saveBatch(growthList);
        boolean success=false;
        if(rr==true){
            if (rs==true){
                success=true;
            }
        };

        return success?1:0;
    }
}
